package com.spzx.channel.service.impl;

import com.spzx.channel.service.IIndexService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IIndexService {

    @Autowired
    RemoteCategoryService remoteCategoryService;

    @Autowired
    RemoteProductService remoteProductService;


    @Override
    public Map<String, Object> index() {
        R<List<CategoryVo>> oneCategoryResult = remoteCategoryService.getOneCategory(SecurityConstants.INNER);//  from-source='inner'
        if(oneCategoryResult.getCode() == R.FAIL){
            throw new ServiceException(oneCategoryResult.getMsg());
        }
        R<List<ProductSku>> topSkuListResult = remoteProductService.getTopSkuSale(SecurityConstants.INNER);
        if(topSkuListResult.getCode() == R.FAIL){
            throw new ServiceException(topSkuListResult.getMsg());
        }

        Map<String, Object> map = new Hashtable();
        map.put("categoryList",oneCategoryResult.getData());
        map.put("productSkuList",topSkuListResult.getData());

        return map;
    }
}
